home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / lisp / modes / fortran.el < prev    next >
Encoding:
Text File  |  1995-03-25  |  49.7 KB  |  1,305 lines

  1. ;;; fortran.el --- Fortran mode for GNU Emacs
  2.  
  3. ;;; Copyright (c) 1986, 1993, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Michael D. Prange <prange@erl.mit.edu>
  6. ;; Maintainer: bug-fortran-mode@erl.mit.edu
  7. ;; Version 1.30.5 (May 20, 1994)
  8. ;; Keywords: languages
  9.  
  10. ;; This file is part of XEmacs.
  11.  
  12. ;; XEmacs is free software; you can redistribute it and/or modify it
  13. ;; under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16.  
  17. ;; XEmacs is distributed in the hope that it will be useful, but
  18. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  20. ;; General Public License for more details.
  21.  
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  24. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  
  26. ;;; Synched up with: FSF 19.28.
  27.  
  28. ;;; Commentary:
  29.  
  30. ;; Fortran mode has been upgraded and is now maintained by Stephen A. Wood
  31. ;; (saw@cebaf.gov).  It now will use either fixed format continuation line
  32. ;; markers (character in 6th column), or tab format continuation line style
  33. ;; (digit after a TAB character.)  A auto-fill mode has been added to
  34. ;; automatically wrap fortran lines that get too long.
  35.  
  36. ;; We acknowledge many contributions and valuable suggestions by
  37. ;; Lawrence R. Dodd, Ralf Fassel, Ralph Finch, Stephen Gildea,
  38. ;; Dr. Anil Gokhale, Ulrich Mueller, Mark Neale, Eric Prestemon, 
  39. ;; Gary Sabot and Richard Stallman.
  40.  
  41. ;;; This file may be used with GNU Emacs version 18.xx if the following
  42. ;;; variable and function substitutions are made.
  43. ;;;  Replace:
  44. ;;;   frame-width                           with screen-width
  45. ;;;   auto-fill-function                    with auto-fill-hook
  46. ;;;   comment-indent-function               with comment-indent-hook
  47. ;;;   (setq unread-command-events (list c)) with (setq unread-command-char c)
  48.  
  49. ;;; Bugs to bug-fortran-mode@erl.mit.edu
  50.  
  51. (defconst fortran-mode-version "version 1.30.5")
  52.  
  53. ;;; Code:
  54.  
  55. ;;;###autoload
  56. (defvar fortran-tab-mode-default nil
  57.   "*Default tabbing/carriage control style for empty files in Fortran mode.
  58. A value of t specifies tab-digit style of continuation control.
  59. A value of nil specifies that continuation lines are marked
  60. with a character in column 6.")
  61.  
  62. ;; Buffer local, used to display mode line.
  63. (defvar fortran-tab-mode-string nil
  64.   "String to appear in mode line when TAB format mode is on.")
  65.  
  66. (defvar fortran-do-indent 3
  67.   "*Extra indentation applied to DO blocks.")
  68.  
  69. (defvar fortran-if-indent 3
  70.   "*Extra indentation applied to IF blocks.")
  71.  
  72. (defvar fortran-structure-indent 3
  73.   "*Extra indentation applied to STRUCTURE, UNION, MAP and INTERFACE blocks.")
  74.  
  75. (defvar fortran-continuation-indent 5
  76.   "*Extra indentation applied to Fortran continuation lines.")
  77.  
  78. (defvar fortran-comment-indent-style 'fixed
  79.   "*nil forces comment lines not to be touched,
  80. 'fixed makes fixed comment indentation to `fortran-comment-line-extra-indent'
  81. columns beyond `fortran-minimum-statement-indent-fixed' (for
  82. `indent-tabs-mode' of nil) or `fortran-minimum-statement-indent-tab' (for
  83. `indent-tabs-mode' of t), and 'relative indents to current
  84. Fortran indentation plus `fortran-comment-line-extra-indent'.")
  85.  
  86. (defvar fortran-comment-line-extra-indent 0
  87.   "*Amount of extra indentation for text within full-line comments.")
  88.  
  89. (defvar comment-line-start nil
  90.   "*Delimiter inserted to start new full-line comment.")
  91.  
  92. (defvar comment-line-start-skip nil
  93.   "*Regexp to match the start of a full-line comment.")
  94.  
  95. (defvar fortran-minimum-statement-indent-fixed 6
  96.   "*Minimum statement indentation for fixed format continuation style.")
  97.  
  98. (defvar fortran-minimum-statement-indent-tab (max tab-width 6)
  99.   "*Minimum statement indentation for TAB format continuation style.")
  100.  
  101. ;; Note that this is documented in the v18 manuals as being a string
  102. ;; of length one rather than a single character.
  103. ;; The code in this file accepts either format for compatibility.
  104. (defvar fortran-comment-indent-char " "
  105.   "*Single-character string inserted for Fortran comment indentation.
  106. Normally a space.")
  107.  
  108. (defvar fortran-line-number-indent 1
  109.   "*Maximum indentation for Fortran line numbers.
  110. 5 means right-justify them within their five-column field.")
  111.  
  112. (defvar fortran-check-all-num-for-matching-do nil
  113.   "*Non-nil causes all numbered lines to be treated as possible DO loop ends.")
  114.  
  115. (defvar fortran-blink-matching-if nil
  116.   "*From a Fortran ENDIF statement, blink the matching IF statement.
  117. Also, from an ENDDO statement, blink on matching DO [WHILE] statement.")
  118.  
  119. (defvar fortran-continuation-string "$"
  120.   "*Single-character string used for Fortran continuation lines.
  121. In fixed format continuation style, this character is inserted in
  122. column 6 by \\[fortran-split-line] to begin a continuation line.
  123. Also, if \\[fortran-indent-line] finds this at the beginning of a line, it will
  124. convert the line into a continuation line of the appropriate style.
  125. Normally $.")
  126.  
  127. (defvar fortran-comment-region "c$$$"
  128.   "*String inserted by \\[fortran-comment-region]\
  129.  at start of each line in region.")
  130.  
  131. (defvar fortran-electric-line-number t
  132.   "*Non-nil causes line number digits to be moved to the correct column as\
  133.  typed.")
  134.  
  135. (defvar fortran-startup-message t
  136.   "*Non-nil displays a startup message when Fortran mode is first called.")
  137.  
  138. (defvar fortran-column-ruler-fixed
  139.   "0   4 6  10        20        30        40        5\
  140. 0        60        70\n\
  141. [   ]|{   |    |    |    |    |    |    |    |    \
  142. |    |    |    |    |}\n"
  143.   "*String displayed above current line by \\[fortran-column-ruler].
  144. This variable used in fixed format mode.")
  145.  
  146. (defvar fortran-column-ruler-tab
  147.   "0       810        20        30        40        5\
  148. 0        60        70\n\
  149. [   ]|  { |    |    |    |    |    |    |    |    \
  150. |    |    |    |    |}\n"
  151.   "*String displayed above current line by \\[fortran-column-ruler].
  152. This variable used in TAB format mode.")
  153.  
  154. (defconst bug-fortran-mode "bug-fortran-mode@erl.mit.edu"
  155.   "Address of mailing list for Fortran mode bugs.")
  156.  
  157. (defvar fortran-mode-syntax-table nil
  158.   "Syntax table in use in Fortran mode buffers.")
  159.  
  160. (defvar fortran-analyze-depth 100
  161.   "Number of lines to scan to determine whether to use fixed or TAB format\
  162.  style.")
  163.  
  164. (defvar fortran-break-before-delimiters t
  165.   "*Non-nil causes `fortran-do-auto-fill' to break lines before delimiters.")
  166.  
  167. (if fortran-mode-syntax-table
  168.     ()
  169.   (setq fortran-mode-syntax-table (make-syntax-table))
  170.   (modify-syntax-entry ?\; "w" fortran-mode-syntax-table)
  171.   (modify-syntax-entry ?\r " " fortran-mode-syntax-table)
  172.   (modify-syntax-entry ?+ "." fortran-mode-syntax-table)
  173.   (modify-syntax-entry ?- "." fortran-mode-syntax-table)
  174.   (modify-syntax-entry ?= "." fortran-mode-syntax-table)
  175.   ;;(modify-syntax-entry ?* "." fortran-mode-syntax-table)
  176.   (modify-syntax-entry ?/ "." fortran-mode-syntax-table)
  177.   (modify-syntax-entry ?\' "\"" fortran-mode-syntax-table)
  178.   (modify-syntax-entry ?\" "\"" fortran-mode-syntax-table)
  179.   (modify-syntax-entry ?\\ "/" fortran-mode-syntax-table)
  180.   (modify-syntax-entry ?. "w" fortran-mode-syntax-table)
  181.   (modify-syntax-entry ?_ "w" fortran-mode-syntax-table)
  182.   ;;(modify-syntax-entry ?\n ">" fortran-mode-syntax-table)
  183.  
  184.   ;; XEmacs: an attempt to make font-lock understand fortran comments.
  185.   (modify-syntax-entry ?\n "> 1" fortran-mode-syntax-table)
  186.   (modify-syntax-entry ?*  ". 2" fortran-mode-syntax-table)
  187.   (modify-syntax-entry ?c  "w 2" fortran-mode-syntax-table)
  188.   (modify-syntax-entry ?C  "w 2" fortran-mode-syntax-table)
  189.  
  190.   )
  191.  
  192. (defvar fortran-mode-map () 
  193.   "Keymap used in Fortran mode.")
  194. (if fortran-mode-map
  195.     ()
  196.   (setq fortran-mode-map (make-sparse-keymap))
  197.   (define-key fortran-mode-map ";" 'fortran-abbrev-start)
  198.   (define-key fortran-mode-map "\C-c;" 'fortran-comment-region)
  199.   (define-key fortran-mode-map "\e\C-a" 'beginning-of-fortran-subprogram)
  200.   (define-key fortran-mode-map "\e\C-e" 'end-of-fortran-subprogram)
  201.   (define-key fortran-mode-map "\e;" 'fortran-indent-comment)
  202.   (define-key fortran-mode-map "\e\C-h" 'mark-fortran-subprogram)
  203.   (define-key fortran-mode-map "\e\n" 'fortran-split-line)
  204.   (define-key fortran-mode-map "\n" 'fortran-indent-new-line)
  205.   (define-key fortran-mode-map "\e\C-q" 'fortran-indent-subprogram)
  206.   (define-key fortran-mode-map "\C-c\C-w" 'fortran-window-create-momentarily)
  207.   (define-key fortran-mode-map "\C-c\C-r" 'fortran-column-ruler)
  208.   (define-key fortran-mode-map "\C-c\C-p" 'fortran-previous-statement)
  209.   (define-key fortran-mode-map "\C-c\C-n" 'fortran-next-statement)
  210.   (define-key fortran-mode-map "\t" 'fortran-indent-line)
  211.   (define-key fortran-mode-map "0" 'fortran-electric-line-number)
  212.   (define-key fortran-mode-map "1" 'fortran-electric-line-number)
  213.   (define-key fortran-mode-map "2" 'fortran-electric-line-number)
  214.   (define-key fortran-mode-map "3" 'fortran-electric-line-number)
  215.   (define-key fortran-mode-map "4" 'fortran-electric-line-number)
  216.   (define-key fortran-mode-map "5" 'fortran-electric-line-number)
  217.   (define-key fortran-mode-map "6" 'fortran-electric-line-number)
  218.   (define-key fortran-mode-map "7" 'fortran-electric-line-number)
  219.   (define-key fortran-mode-map "8" 'fortran-electric-line-number)
  220.   (define-key fortran-mode-map "9" 'fortran-electric-line-number))
  221.  
  222. (defvar fortran-mode-abbrev-table nil)
  223. (if fortran-mode-abbrev-table
  224.     ()
  225.   (let ((ac abbrevs-changed))
  226.     (define-abbrev-table 'fortran-mode-abbrev-table ())
  227.     (define-abbrev fortran-mode-abbrev-table  ";au"  "automatic" nil)
  228.     (define-abbrev fortran-mode-abbrev-table  ";b"   "byte" nil)
  229.     (define-abbrev fortran-mode-abbrev-table  ";bd"  "block data" nil)
  230.     (define-abbrev fortran-mode-abbrev-table  ";ch"  "character" nil)
  231.     (define-abbrev fortran-mode-abbrev-table  ";cl"  "close" nil)
  232.     (define-abbrev fortran-mode-abbrev-table  ";c"   "continue" nil)
  233.     (define-abbrev fortran-mode-abbrev-table  ";cm"  "common" nil)
  234.     (define-abbrev fortran-mode-abbrev-table  ";cx"  "complex" nil)
  235.     (define-abbrev fortran-mode-abbrev-table  ";df"  "define" nil)
  236.     (define-abbrev fortran-mode-abbrev-table  ";di"  "dimension" nil)
  237.     (define-abbrev fortran-mode-abbrev-table  ";do"  "double" nil)
  238.     (define-abbrev fortran-mode-abbrev-table  ";dc"  "double complex" nil)
  239.     (define-abbrev fortran-mode-abbrev-table  ";dp"  "double precision" nil)
  240.     (define-abbrev fortran-mode-abbrev-table  ";dw"  "do while" nil)
  241.     (define-abbrev fortran-mode-abbrev-table  ";e"   "else" nil)
  242.     (define-abbrev fortran-mode-abbrev-table  ";ed"  "enddo" nil)
  243.     (define-abbrev fortran-mode-abbrev-table  ";el"  "elseif" nil)
  244.     (define-abbrev fortran-mode-abbrev-table  ";en"  "endif" nil)
  245.     (define-abbrev fortran-mode-abbrev-table  ";eq"  "equivalence" nil)
  246.     (define-abbrev fortran-mode-abbrev-table  ";ew"  "endwhere" nil)
  247.     (define-abbrev fortran-mode-abbrev-table  ";ex"  "external" nil)
  248.     (define-abbrev fortran-mode-abbrev-table  ";ey"  "entry" nil)
  249.     (define-abbrev fortran-mode-abbrev-table  ";f"   "format" nil)
  250.     (define-abbrev fortran-mode-abbrev-table  ";fa"  ".false." nil)
  251.     (define-abbrev fortran-mode-abbrev-table  ";fu"  "function" nil)
  252.     (define-abbrev fortran-mode-abbrev-table  ";g"   "goto" nil)
  253.     (define-abbrev fortran-mode-abbrev-table  ";im"  "implicit" nil)
  254.     (define-abbrev fortran-mode-abbrev-table  ";ib"  "implicit byte" nil)
  255.     (define-abbrev fortran-mode-abbrev-table  ";ic"  "implicit complex" nil)
  256.     (define-abbrev fortran-mode-abbrev-table  ";ich" "implicit character" nil)
  257.     (define-abbrev fortran-mode-abbrev-table  ";ii"  "implicit integer" nil)
  258.     (define-abbrev fortran-mode-abbrev-table  ";il"  "implicit logical" nil)
  259.     (define-abbrev fortran-mode-abbrev-table  ";ir"  "implicit real" nil)
  260.     (define-abbrev fortran-mode-abbrev-table  ";inc" "include" nil)
  261.     (define-abbrev fortran-mode-abbrev-table  ";in"  "integer" nil)
  262.     (define-abbrev fortran-mode-abbrev-table  ";intr" "intrinsic" nil)
  263.     (define-abbrev fortran-mode-abbrev-table  ";l"   "logical" nil)
  264.     (define-abbrev fortran-mode-abbrev-table  ";n"   "namelist" nil)
  265.     (define-abbrev fortran-mode-abbrev-table  ";o"   "open" nil) ; was ;op
  266.     (define-abbrev fortran-mode-abbrev-table  ";pa"  "parameter" nil)
  267.     (define-abbrev fortran-mode-abbrev-table  ";pr"  "program" nil)
  268.     (define-abbrev fortran-mode-abbrev-table  ";ps"  "pause" nil)
  269.     (define-abbrev fortran-mode-abbrev-table  ";p"   "print" nil)
  270.     (define-abbrev fortran-mode-abbrev-table  ";rc"  "record" nil)
  271.     (define-abbrev fortran-mode-abbrev-table  ";re"  "real" nil)
  272.     (define-abbrev fortran-mode-abbrev-table  ";r"   "read" nil)
  273.     (define-abbrev fortran-mode-abbrev-table  ";rt"  "return" nil)
  274.     (define-abbrev fortran-mode-abbrev-table  ";rw"  "rewind" nil)
  275.     (define-abbrev fortran-mode-abbrev-table  ";s"   "stop" nil)
  276.     (define-abbrev fortran-mode-abbrev-table  ";sa"  "save" nil)
  277.     (define-abbrev fortran-mode-abbrev-table  ";st"  "structure" nil)
  278.     (define-abbrev fortran-mode-abbrev-table  ";sc"  "static" nil)
  279.     (define-abbrev fortran-mode-abbrev-table  ";su"  "subroutine" nil)
  280.     (define-abbrev fortran-mode-abbrev-table  ";tr"  ".true." nil)
  281.     (define-abbrev fortran-mode-abbrev-table  ";ty"  "type" nil)
  282.     (define-abbrev fortran-mode-abbrev-table  ";vo"  "volatile" nil)
  283.     (define-abbrev fortran-mode-abbrev-table  ";w"   "write" nil)
  284.     (define-abbrev fortran-mode-abbrev-table  ";wh"  "where" nil)
  285.     (setq abbrevs-changed ac)))
  286.  
  287. ;;;###autoload
  288. (defun fortran-mode ()
  289.   "Major mode for editing Fortran code.
  290. \\[fortran-indent-line] indents the current Fortran line correctly. 
  291. DO statements must not share a common CONTINUE.
  292.  
  293. Type ;? or ;\\[help-command] to display a list of built-in\
  294.  abbrevs for Fortran keywords.
  295.  
  296. Key definitions:
  297. \\{fortran-mode-map}
  298.  
  299. Variables controlling indentation style and extra features:
  300.  
  301.  comment-start
  302.     Normally nil in Fortran mode.  If you want to use comments
  303.     starting with `!', set this to the string \"!\".
  304.  fortran-do-indent
  305.     Extra indentation within do blocks.  (default 3)
  306.  fortran-if-indent
  307.     Extra indentation within if blocks.  (default 3)
  308.  fortran-structure-indent
  309.     Extra indentation within structure, union, map and interface blocks.
  310.     (default 3)
  311.  fortran-continuation-indent
  312.     Extra indentation applied to continuation statements.  (default 5)
  313.  fortran-comment-line-extra-indent
  314.     Amount of extra indentation for text within full-line comments. (default 0)
  315.  fortran-comment-indent-style
  316.     nil    means don't change indentation of text in full-line comments,
  317.     fixed  means indent that text at `fortran-comment-line-extra-indent' beyond
  318.            the value of `fortran-minimum-statement-indent-fixed' (for fixed
  319.            format continuation style) or `fortran-minimum-statement-indent-tab'
  320.            (for TAB format continuation style).
  321.     relative  means indent at `fortran-comment-line-extra-indent' beyond the
  322.            indentation for a line of code.
  323.     (default 'fixed)
  324.  fortran-comment-indent-char
  325.     Single-character string to be inserted instead of space for
  326.     full-line comment indentation.  (default \" \")
  327.  fortran-minimum-statement-indent-fixed
  328.     Minimum indentation for Fortran statements in fixed format mode. (def.6)
  329.  fortran-minimum-statement-indent-tab
  330.     Minimum indentation for Fortran statements in TAB format mode. (default 9)
  331.  fortran-line-number-indent
  332.     Maximum indentation for line numbers.  A line number will get
  333.     less than this much indentation if necessary to avoid reaching
  334.     column 5.  (default 1)
  335.  fortran-check-all-num-for-matching-do
  336.     Non-nil causes all numbered lines to be treated as possible \"continue\"
  337.     statements.  (default nil)
  338.  fortran-blink-matching-if 
  339.     From a Fortran ENDIF statement, blink the matching IF statement.
  340.     Also, from an ENDDO statement, blink on matching DO [WHILE] statement.
  341.     (default nil)
  342.  fortran-continuation-string
  343.     Single-character string to be inserted in column 5 of a continuation
  344.     line.  (default \"$\")
  345.  fortran-comment-region
  346.     String inserted by \\[fortran-comment-region] at start of each line in 
  347.     region.  (default \"c$$$\")
  348.  fortran-electric-line-number
  349.     Non-nil causes line number digits to be moved to the correct column 
  350.     as typed.  (default t)
  351.  fortran-break-before-delimiters
  352.     Non-nil causes `fortran-do-auto-fill' breaks lines before delimiters.
  353.     (default t)
  354.  fortran-startup-message
  355.     Set to nil to inhibit message first time Fortran mode is used.
  356.  
  357. Turning on Fortran mode calls the value of the variable `fortran-mode-hook'
  358. with no args, if that value is non-nil."
  359.   (interactive)
  360.   (kill-all-local-variables)
  361.   (if fortran-startup-message
  362.       (message "Emacs Fortran mode %s.  Bugs to %s"
  363.            fortran-mode-version bug-fortran-mode))
  364.   (setq fortran-startup-message nil)
  365.   (setq local-abbrev-table fortran-mode-abbrev-table)
  366.   (set-syntax-table fortran-mode-syntax-table)
  367.   (make-local-variable 'fortran-break-before-delimiters)
  368.   (setq fortran-break-before-delimiters t)
  369.   (make-local-variable 'indent-line-function)
  370.   (setq indent-line-function 'fortran-indent-line)
  371.   (make-local-variable 'comment-indent-function)
  372.   (setq comment-indent-function 'fortran-comment-hook)
  373.   (make-local-variable 'comment-line-start-skip)
  374.   (setq comment-line-start-skip
  375.     "^[Cc*]\\(\\([^ \t\n]\\)\\2\\2*\\)?[ \t]*\\|^#.*")
  376.   (make-local-variable 'comment-line-start)
  377.   (setq comment-line-start "c")
  378.   (make-local-variable 'comment-start-skip)
  379.   (setq comment-start-skip "![ \t]*")
  380.   (make-local-variable 'comment-start)
  381.   (setq comment-start nil)
  382.   (make-local-variable 'require-final-newline)
  383.   (setq require-final-newline t)
  384.   (make-local-variable 'abbrev-all-caps)
  385.   (setq abbrev-all-caps t)
  386.   (make-local-variable 'indent-tabs-mode)
  387.   (setq indent-tabs-mode nil)
  388. ;;;(setq abbrev-mode t) ; ?? (abbrev-mode 1) instead??
  389.   (setq fill-column 72) ; Already local?
  390.   (use-local-map fortran-mode-map)
  391.   (setq mode-name "Fortran")
  392.   (setq major-mode 'fortran-mode)
  393. ;;;(make-local-variable 'fortran-tab-mode)
  394.   (make-local-variable 'fortran-comment-line-extra-indent)
  395.   (make-local-variable 'fortran-minimum-statement-indent-fixed)
  396.   (make-local-variable 'fortran-minimum-statement-indent-tab)
  397.   (make-local-variable 'fortran-column-ruler-fixed)
  398.   (make-local-variable 'fortran-column-ruler-tab)
  399.   (make-local-variable 'fortran-tab-mode-string)
  400.   (setq fortran-tab-mode-string " TAB-format")
  401.   (setq indent-tabs-mode (fortran-analyze-file-format))
  402.   (run-hooks 'fortran-mode-hook))
  403.  
  404. (defun fortran-comment-hook ()
  405.   (save-excursion
  406.     (skip-chars-backward " \t")
  407.     (max (+ 1 (current-column))
  408.      comment-column)))
  409.  
  410. (defun fortran-indent-comment ()
  411.   "Align or create comment on current line.
  412. Existing comments of all types are recognized and aligned.
  413. If the line has no comment, a side-by-side comment is inserted and aligned
  414. if the value of  comment-start  is not nil.
  415. Otherwise, a separate-line comment is inserted, on this line
  416. or on a new line inserted before this line if this line is not blank."
  417.   (interactive)
  418.   (beginning-of-line)
  419.   ;; Recognize existing comments of either kind.
  420.   (cond ((looking-at comment-line-start-skip)
  421.      (fortran-indent-line))
  422.     ((fortran-find-comment-start-skip) ; catches any inline comment and
  423.                     ; leaves point after comment-start-skip
  424.      (if comment-start-skip
  425.          (progn (goto-char (match-beginning 0))
  426.             (if (not (= (current-column) (fortran-comment-hook)))
  427.             (progn (delete-horizontal-space)
  428.                    (indent-to (fortran-comment-hook)))))
  429.        (end-of-line)))        ; otherwise goto end of line or sth else?
  430.     ;; No existing comment.
  431.     ;; If side-by-side comments are defined, insert one,
  432.     ;; unless line is now blank.
  433.     ((and comment-start (not (looking-at "^[ \t]*$")))
  434.      (end-of-line)
  435.      (delete-horizontal-space)
  436.      (indent-to (fortran-comment-hook))
  437.      (insert comment-start))
  438.     ;; Else insert separate-line comment, making a new line if nec.
  439.     (t
  440.      (if (looking-at "^[ \t]*$")
  441.          (delete-horizontal-space)
  442.        (beginning-of-line)
  443.        (insert "\n")
  444.        (forward-char -1))
  445.      (insert comment-line-start)
  446.      (insert-char (if (stringp fortran-comment-indent-char)
  447.               (aref fortran-comment-indent-char 0)
  448.             fortran-comment-indent-char)
  449.               (- (calculate-fortran-indent) (current-column))))))
  450.  
  451. (defun fortran-comment-region (beg-region end-region arg)
  452.   "Comments every line in the region.
  453. Puts fortran-comment-region at the beginning of every line in the region. 
  454. BEG-REGION and END-REGION are args which specify the region boundaries. 
  455. With non-nil ARG, uncomments the region."
  456.   (interactive "*r\nP")
  457.   (let ((end-region-mark (make-marker)) (save-point (point-marker)))
  458.     (set-marker end-region-mark end-region)
  459.     (goto-char beg-region)
  460.     (beginning-of-line)
  461.     (if (not arg)            ;comment the region
  462.     (progn (insert fortran-comment-region)
  463.            (while (and  (= (forward-line 1) 0)
  464.                 (< (point) end-region-mark))
  465.          (insert fortran-comment-region)))
  466.       (let ((com (regexp-quote fortran-comment-region))) ;uncomment the region
  467.     (if (looking-at com)
  468.         (delete-region (point) (match-end 0)))
  469.     (while (and  (= (forward-line 1) 0)
  470.              (< (point) end-region-mark))
  471.       (if (looking-at com)
  472.           (delete-region (point) (match-end 0))))))
  473.     (goto-char save-point)
  474.     (set-marker end-region-mark nil)
  475.     (set-marker save-point nil)))
  476.  
  477. (defun fortran-abbrev-start ()
  478.   "Typing ;\\[help-command] or ;? lists all the Fortran abbrevs. 
  479. Any other key combination is executed normally."
  480.   (interactive)
  481.   (let (e c)
  482.     (insert last-command-char)
  483.     (setq e (next-command-event)
  484.       c (event-to-character e))
  485.     ;; insert char if not equal to `?'
  486.     (if (or (= c ??) (eq c help-char))
  487.     (fortran-abbrev-help)
  488.       (setq unread-command-event e))))
  489.  
  490. (defun fortran-abbrev-help ()
  491.   "List the currently defined abbrevs in Fortran mode."
  492.   (interactive)
  493.   (message "Listing abbrev table...")
  494.   (display-buffer (fortran-prepare-abbrev-list-buffer))
  495.   (message "Listing abbrev table...done"))
  496.  
  497. (defun fortran-prepare-abbrev-list-buffer ()
  498.   (save-excursion
  499.     (set-buffer (get-buffer-create "*Abbrevs*"))
  500.     (erase-buffer)
  501.     (insert-abbrev-table-description 'fortran-mode-abbrev-table t)
  502.     (goto-char (point-min))
  503.     (set-buffer-modified-p nil)
  504.     (edit-abbrevs-mode))
  505.   (get-buffer-create "*Abbrevs*"))
  506.  
  507. (defun fortran-column-ruler ()
  508.   "Inserts a column ruler momentarily above current line, till next keystroke.
  509. The ruler is defined by the value of `fortran-column-ruler-fixed' when in fixed
  510. format mode, and `fortran-column-ruler-tab' when in TAB format mode.
  511. The key typed is executed unless it is SPC."
  512.   (interactive)
  513.   (momentary-string-display 
  514.    (if indent-tabs-mode
  515.        fortran-column-ruler-tab
  516.      fortran-column-ruler-fixed)
  517.    (save-excursion
  518.      (beginning-of-line) 
  519.      (if (eq (window-start (selected-window))
  520.          (window-point (selected-window)))
  521.      (progn (forward-line) (point))
  522.        (point)))
  523.    nil "Type SPC or any command to erase ruler."))
  524.  
  525. (defun fortran-window-create ()
  526.   "Makes the window 72 columns wide.
  527. See also `fortran-window-create-momentarily'."
  528.   (interactive)
  529.   (condition-case error
  530.       (progn
  531.     (let ((window-min-width 2))
  532.       (if (< (window-width) (frame-width))
  533.           (enlarge-window-horizontally (- (frame-width)
  534.                           (window-width) 1)))
  535.       (split-window-horizontally 73)
  536.       (other-window 1)
  537.       (switch-to-buffer " fortran-window-extra" t)
  538.       (select-window (previous-window))))
  539.     (error (message "No room for Fortran window.")
  540.        'error)))
  541.  
  542. (defun fortran-window-create-momentarily (&optional arg)
  543.   "Momentarily makes the window 72 columns wide.
  544. Optional ARG non-nil and non-unity disables the momentary feature.
  545. See also `fortran-window-create'."
  546.   (interactive "p")
  547.   (if (or (not arg)
  548.       (= arg 1))
  549.       (save-window-excursion
  550.     (if (not (equal (fortran-window-create) 'error))
  551.         (progn (message "Type SPC to continue editing.")
  552.            (let ((char (next-command-event)))
  553.              (or (equal (event-to-character char) ? )
  554.              (setq unread-command-event char))))))
  555.     (fortran-window-create)))
  556.  
  557. (defun fortran-split-line ()
  558.   "Break line at point and insert continuation marker and alignment."
  559.   (interactive)
  560.   (delete-horizontal-space)
  561.   (if (save-excursion (beginning-of-line) (looking-at comment-line-start-skip))
  562.       (insert "\n" comment-line-start " ")
  563.     (if indent-tabs-mode
  564.     (progn 
  565.       (insert "\n\t")
  566.       (insert-char (fortran-numerical-continuation-char) 1))
  567.       (insert "\n " fortran-continuation-string)));Space after \n important
  568.   (fortran-indent-line))        ;when the cont string is C, c or *.
  569.  
  570. (defun fortran-numerical-continuation-char ()
  571.   "Return a digit for tab-digit style of continuation lines.
  572. If, previous line is a tab-digit continuation line, returns that digit
  573. plus one.  Otherwise return 1.  Zero not allowed."
  574.   (save-excursion
  575.     (forward-line -1)
  576.     (if (looking-at "\t[1-9]")
  577.     (+ ?1 (% (- (char-after (+ (point) 1)) ?0) 9))
  578.       ?1)))
  579.  
  580. (defun delete-horizontal-regexp (chars)
  581.   "Delete all characters in CHARS around point.
  582. CHARS is like the inside of a [...] in a regular expression
  583. except that ] is never special and \ quotes ^, - or \."
  584.   (interactive "*s")
  585.   (skip-chars-backward chars)
  586.   (delete-region (point) (progn (skip-chars-forward chars) (point))))
  587.  
  588. (defun fortran-electric-line-number (arg)
  589.   "Self insert, but if part of a Fortran line number indent it automatically.
  590. Auto-indent does not happen if a numeric arg is used."
  591.   (interactive "P")
  592.   (if (or arg (not fortran-electric-line-number))
  593.       (if arg 
  594.       (self-insert-command (prefix-numeric-value arg))
  595.     (self-insert-command 1))
  596.     (if (or (and (= 5 (current-column))
  597.          (save-excursion
  598.            (beginning-of-line)
  599.            (looking-at "     ")));In col 5 with only spaces to left.
  600.         (and (= (if indent-tabs-mode
  601.           fortran-minimum-statement-indent-tab
  602.         fortran-minimum-statement-indent-fixed) (current-column))
  603.          (save-excursion
  604.            (beginning-of-line)
  605.            (looking-at "\t"));In col 8 with a single tab to the left.
  606.          (not (or (eq last-command 'fortran-indent-line)
  607.               (eq last-command
  608.                   'fortran-indent-new-line))))
  609.         (save-excursion
  610.           (re-search-backward "[^ \t0-9]"
  611.                   (save-excursion
  612.                     (beginning-of-line)
  613.                     (point))
  614.                   t)) ;not a line number
  615.         (looking-at "[0-9]")    ;within a line number
  616.         )
  617.     (self-insert-command (prefix-numeric-value arg))
  618.       (skip-chars-backward " \t")
  619.       (insert last-command-char)
  620.       (fortran-indent-line))))
  621.  
  622. (defun beginning-of-fortran-subprogram ()
  623.   "Moves point to the beginning of the current Fortran subprogram."
  624.   (interactive)
  625.   (let ((case-fold-search t))
  626.     (beginning-of-line -1)
  627.     (re-search-backward "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]" nil 'move)
  628.     (if (looking-at "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]")
  629.     (forward-line 1))))
  630.  
  631. (defun end-of-fortran-subprogram ()
  632.   "Moves point to the end of the current Fortran subprogram."
  633.   (interactive)
  634.   (let ((case-fold-search t))
  635.     (beginning-of-line 2)
  636.     (re-search-forward "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]" nil 'move)
  637.     (goto-char (match-beginning 0))
  638.     (forward-line 1)))
  639.  
  640. (defun mark-fortran-subprogram ()
  641.   "Put mark at end of Fortran subprogram, point at beginning. 
  642. The marks are pushed."
  643.   (interactive)
  644.   (end-of-fortran-subprogram)
  645.   (push-mark (point))
  646.   (beginning-of-fortran-subprogram))
  647.  
  648. (defun fortran-previous-statement ()
  649.   "Moves point to beginning of the previous Fortran statement.
  650. Returns `first-statement' if that statement is the first
  651. non-comment Fortran statement in the file, and nil otherwise."
  652.   (interactive)
  653.   (let (not-first-statement continue-test)
  654.     (beginning-of-line)
  655.     (setq continue-test
  656.       (and
  657.        (not (looking-at comment-line-start-skip))
  658.        (or (looking-at
  659.             (concat "[ \t]*" (regexp-quote fortran-continuation-string)))
  660.            (or (looking-at "     [^ 0\n]")
  661.            (looking-at "\t[1-9]")))))
  662.     (while (and (setq not-first-statement (= (forward-line -1) 0))
  663.         (or (looking-at comment-line-start-skip)
  664.             (looking-at "[ \t]*$")
  665.             (looking-at "     [^ 0\n]")
  666.             (looking-at "\t[1-9]")
  667.             (looking-at (concat "[ \t]*"  comment-start-skip)))))
  668.     (cond ((and continue-test
  669.         (not not-first-statement))
  670.        (message "Incomplete continuation statement."))
  671.       (continue-test    
  672.        (fortran-previous-statement))
  673.       ((not not-first-statement)
  674.        'first-statement))))
  675.  
  676. (defun fortran-next-statement ()
  677.   "Moves point to beginning of the next Fortran statement.
  678. Returns `last-statement' if that statement is the last
  679. non-comment Fortran statement in the file, and nil otherwise."
  680.   (interactive)
  681.   (let (not-last-statement)
  682.     (beginning-of-line)
  683.     (while (and (setq not-last-statement
  684.               (and (= (forward-line 1) 0)
  685.                (not (eobp))))
  686.          (or (looking-at comment-line-start-skip)
  687.              (looking-at "[ \t]*$")
  688.              (looking-at "     [^ 0\n]")
  689.              (looking-at "\t[1-9]")
  690.              (looking-at (concat "[ \t]*"  comment-start-skip)))))
  691.     (if (not not-last-statement)
  692.      'last-statement)))
  693.  
  694. (defun fortran-blink-matching-if ()
  695.   "From a Fortran ENDIF statement, blink the matching IF statement."
  696.   (let ((count 1) (top-of-window (window-start)) matching-if
  697.     (endif-point (point)) message)
  698.     (if (save-excursion (beginning-of-line)
  699.             (skip-chars-forward " \t0-9")
  700.             (looking-at "end[ \t]*if\\b"))
  701.     (progn
  702.       (save-excursion
  703.         (while (and (not (= count 0))
  704.             (not (eq (fortran-previous-statement)
  705.                  'first-statement))
  706.             (not (looking-at
  707.                   "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]")))
  708.                     ; Keep local to subprogram
  709.           (skip-chars-forward " \t0-9")
  710.           (cond ((looking-at "if[ \t]*(")
  711.              (save-excursion
  712.                (if (or
  713.                 (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t(=a-z0-9]")
  714.                 (let (then-test);multi-line if-then
  715.                   (while
  716.                   (and (= (forward-line 1) 0)
  717.                     ;search forward for then
  718.                        (or (looking-at "     [^ 0\n]")
  719.                        (looking-at "\t[1-9]"))
  720.                        (not
  721.                     (setq
  722.                      then-test
  723.                      (looking-at
  724.                       ".*then\\b[ \t]*[^ \t(=a-z0-9]")))))
  725.                   then-test))
  726.                (setq count (- count 1)))))
  727.             ((looking-at "end[ \t]*if\\b")
  728.              (setq count (+ count 1)))))
  729.         (if (not (= count 0))
  730.         (setq message "No matching if.")
  731.           (if (< (point) top-of-window)
  732.           (setq message (concat "Matches " (buffer-substring
  733.                             (progn (beginning-of-line)
  734.                                (point))
  735.                             (progn (end-of-line)
  736.                                (point)))))
  737.         (setq matching-if (point)))))
  738.       (if message
  739.           (message "%s" message)
  740.         (goto-char matching-if)
  741.         (sit-for 1)
  742.         (goto-char endif-point))))))
  743.  
  744. (defun fortran-blink-matching-do ()
  745.   ;; From a Fortran ENDDO statement, blink on the matching DO or DO WHILE
  746.   ;; statement.  This is basically copied from fortran-blink-matching-if.
  747.   (let ((count 1) (top-of-window (window-start)) matching-do
  748.     (enddo-point (point)) message)
  749.     (if (save-excursion (beginning-of-line)
  750.             (skip-chars-forward " \t0-9")
  751.             (looking-at "end[ \t]*do\\b"))
  752.     (progn
  753.       (save-excursion
  754.         (while (and (not (= count 0))
  755.             (not (eq (fortran-previous-statement)
  756.                  'first-statement))
  757.             (not (looking-at
  758.                   "^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]")))
  759.                     ; Keep local to subprogram
  760.           (skip-chars-forward " \t0-9")
  761.           (cond ((looking-at "do[ \t]+[^0-9]")
  762.                      (setq count (- count 1)))
  763.             ((looking-at "end[ \t]*do\\b")
  764.              (setq count (+ count 1)))))
  765.         (if (not (= count 0))
  766.         (setq message "No matching do")
  767.           (if (< (point) top-of-window)
  768.           (setq message (concat "Matches " (buffer-substring
  769.                             (progn (beginning-of-line)
  770.                                (point))
  771.                             (progn (end-of-line)
  772.                                (point)))))
  773.         (setq matching-do (point)))))
  774.       (if message
  775.           (message "%s" message)
  776.         (goto-char matching-do)
  777.         (sit-for 1)
  778.         (goto-char enddo-point))))))
  779.  
  780. (defun fortran-indent-line ()
  781.   "Indents current Fortran line based on its contents and on previous lines."
  782.   (interactive)
  783.   (let ((cfi (calculate-fortran-indent)))
  784.     (save-excursion
  785.       (beginning-of-line)
  786.       (if (or (not (= cfi (fortran-current-line-indentation)))
  787.           (and (re-search-forward "^[ \t]*[0-9]+" (+ (point) 4) t)
  788.            (not (fortran-line-number-indented-correctly-p))))
  789.       (fortran-indent-to-column cfi)
  790.     (beginning-of-line)
  791.     (if (and (not (looking-at comment-line-start-skip))
  792.          (fortran-find-comment-start-skip))
  793.         (fortran-indent-comment))))
  794.     ;; Never leave point in left margin.
  795.     (if (< (current-column) cfi)
  796.     (move-to-column cfi))
  797.     (if (and auto-fill-function
  798.          (> (save-excursion (end-of-line) (current-column)) fill-column))
  799.     (save-excursion
  800.       (end-of-line)
  801.       (fortran-do-auto-fill)))
  802.     (if fortran-blink-matching-if
  803.     (progn
  804.       (fortran-blink-matching-if)
  805.       (fortran-blink-matching-do)))))
  806.  
  807. (defun fortran-indent-new-line ()
  808.   "Reindent the current Fortran line, insert a newline and indent the newline.
  809. An abbrev before point is expanded if `abbrev-mode' is non-nil."
  810.   (interactive)
  811.   (if abbrev-mode (expand-abbrev))
  812.   (save-excursion
  813.     (beginning-of-line)
  814.     (skip-chars-forward " \t")
  815.     (if (or (looking-at "[0-9]")    ;Reindent only where it is most
  816.         (looking-at "end")        ;likely to be necessary
  817.         (looking-at "else")
  818.         (looking-at (regexp-quote fortran-continuation-string)))
  819.     (fortran-indent-line)))
  820.   (newline)
  821.   (fortran-indent-line))
  822.  
  823. (defun fortran-indent-subprogram ()
  824.   "Properly indents the Fortran subprogram which contains point."
  825.   (interactive)
  826.   (save-excursion
  827.     (mark-fortran-subprogram)
  828.     (message "Indenting subprogram...")
  829.     (indent-region (point) (mark t) nil)) ; XEmacs change
  830.   (message "Indenting subprogram...done."))
  831.  
  832. (defun calculate-fortran-indent ()
  833.   "Calculates the Fortran indent column based on previous lines."
  834.   (let (icol first-statement (case-fold-search t)
  835.          (fortran-minimum-statement-indent
  836.           (if indent-tabs-mode
  837.           fortran-minimum-statement-indent-tab
  838.         fortran-minimum-statement-indent-fixed)))
  839.     (save-excursion
  840.       (setq first-statement (fortran-previous-statement))
  841.       (if first-statement
  842.       (setq icol fortran-minimum-statement-indent)
  843.     (progn
  844.       (if (= (point) (point-min))
  845.           (setq icol fortran-minimum-statement-indent)
  846.         (setq icol (fortran-current-line-indentation)))
  847.       (skip-chars-forward " \t0-9")
  848.       (cond ((looking-at "if[ \t]*(")
  849.          (if (or (looking-at ".*)[ \t]*then\\b[ \t]*[^ \t_$(=a-z0-9]")
  850.              (let (then-test)    ;multi-line if-then
  851.                (while (and (= (forward-line 1) 0)
  852.                        ;;search forward for then
  853.                        (or (looking-at "     [^ 0\n]")
  854.                        (looking-at "\t[1-9]"))
  855.                        (not (setq then-test (looking-at
  856.                                  ".*then\\b[ \t]\
  857. *[^ \t_$(=a-z0-9]")))))
  858.                then-test))
  859.              (setq icol (+ icol fortran-if-indent))))
  860.         ((looking-at "\\(else\\|elseif\\)\\b")
  861.          (setq icol (+ icol fortran-if-indent)))
  862.         ((looking-at "select[ \t]*case[ \t](.*)\\b")
  863.          (setq icol (+ icol fortran-if-indent)))
  864.         ((looking-at "case[ \t]*(.*)[ \t]*\n")
  865.          (setq icol (+ icol fortran-if-indent)))
  866.         ((looking-at "case[ \t]*default\\b")
  867.          (setq icol (+ icol fortran-if-indent)))
  868.         ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
  869.          (setq icol (+ icol fortran-if-indent)))
  870.         ((looking-at "where[ \t]*(.*)[ \t]*\n")
  871.          (setq icol (+ icol fortran-if-indent)))
  872.         ((looking-at "do\\b")
  873.          (setq icol (+ icol fortran-do-indent)))
  874.         ((looking-at
  875.           "\\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
  876.          (setq icol (+ icol fortran-structure-indent)))
  877.         ((looking-at "end\\b[ \t]*[^ \t=(a-z]")
  878.          ;; Previous END resets indent to minimum
  879.          (setq icol fortran-minimum-statement-indent))))))
  880.     (save-excursion
  881.       (beginning-of-line)
  882.       (cond ((looking-at "[ \t]*$"))
  883.         ((looking-at comment-line-start-skip)
  884.          (cond ((eq fortran-comment-indent-style 'relative)
  885.             (setq icol (+ icol fortran-comment-line-extra-indent)))
  886.            ((eq fortran-comment-indent-style 'fixed)
  887.             (setq icol (+ fortran-minimum-statement-indent
  888.                   fortran-comment-line-extra-indent))))
  889.          (setq fortran-minimum-statement-indent 0))
  890.         ((or (looking-at (concat "[ \t]*"
  891.                      (regexp-quote
  892.                       fortran-continuation-string)))
  893.          (looking-at "     [^ 0\n]")
  894.          (looking-at "\t[1-9]"))
  895.          (setq icol (+ icol fortran-continuation-indent)))
  896.         ((looking-at "[ \t]*#")    ; Check for cpp directive.
  897.          (setq fortran-minimum-statement-indent 0 icol 0))
  898.         (first-statement)
  899.         ((and fortran-check-all-num-for-matching-do
  900.           (looking-at "[ \t]*[0-9]+")
  901.           (fortran-check-for-matching-do))
  902.          (setq icol (- icol fortran-do-indent)))
  903.         (t
  904.          (skip-chars-forward " \t0-9")
  905.          (cond ((looking-at "end[ \t]*if\\b")
  906.             (setq icol (- icol fortran-if-indent)))
  907.            ((looking-at "\\(else\\|elseif\\)\\b")
  908.             (setq icol (- icol fortran-if-indent)))
  909.                    ((looking-at "case[ \t]*(.*)[ \t]*\n")
  910.             (setq icol (- icol fortran-if-indent)))
  911.                    ((looking-at "case[ \t]*default\\b")
  912.             (setq icol (- icol fortran-if-indent)))
  913.            ((looking-at "\\(otherwise\\|else[ \t]*where\\)\\b")
  914.             (setq icol (- icol fortran-if-indent)))
  915.            ((looking-at "end[ \t]*where\\b")
  916.             (setq icol (- icol fortran-if-indent)))
  917.            ((and (looking-at "continue\\b")
  918.              (fortran-check-for-matching-do))
  919.             (setq icol (- icol fortran-do-indent)))
  920.            ((looking-at "end[ \t]*do\\b")
  921.             (setq icol (- icol fortran-do-indent)))
  922.            ((looking-at
  923.              "end[ \t]*\
  924. \\(structure\\|union\\|map\\|interface\\)\\b[ \t]*[^ \t=(a-z]")
  925.             (setq icol (- icol fortran-structure-indent)))
  926.            ((looking-at
  927.              "end[ \t]*select\\b[ \t]*[^ \t=(a-z]")
  928.             (setq icol (- icol fortran-if-indent)))
  929.            ((and (looking-at "end\\b[ \t]*[^ \t=(a-z]")
  930.              (not (= icol fortran-minimum-statement-indent)))
  931.              (message "Warning: `end' not in column %d.  Probably\
  932.  an unclosed block." fortran-minimum-statement-indent))))))
  933.     (max fortran-minimum-statement-indent icol)))
  934.  
  935. (defun fortran-current-line-indentation ()
  936.   "Indentation of current line, ignoring Fortran line number or continuation.
  937. This is the column position of the first non-whitespace character
  938. aside from the line number and/or column 5/8 line-continuation character.
  939. For comment lines, returns indentation of the first
  940. non-indentation text within the comment."
  941.   (save-excursion
  942.     (beginning-of-line)
  943.     (cond ((looking-at comment-line-start-skip)
  944.        (goto-char (match-end 0))
  945.        (skip-chars-forward
  946.         (if (stringp fortran-comment-indent-char)
  947.         fortran-comment-indent-char
  948.           (char-to-string fortran-comment-indent-char))))
  949.       ((or (looking-at "     [^ 0\n]")
  950.            (looking-at "\t[1-9]"))
  951.        (goto-char (match-end 0)))
  952.       (t
  953.        ;; Move past line number.
  954.        (skip-chars-forward "[ \t0-9]");From Uli
  955.        ))
  956.     ;; Move past whitespace.
  957.     (skip-chars-forward " \t")
  958.     (current-column)))
  959.  
  960. (defun fortran-indent-to-column (col)
  961.   "Indents current line with spaces to column COL.
  962. notes: 1) A non-zero/non-blank character in column 5 indicates a continuation
  963.           line, and this continuation character is retained on indentation;
  964.        2) If `fortran-continuation-string' is the first non-whitespace
  965.           character, this is a continuation line;
  966.        3) A non-continuation line which has a number as the first
  967.           non-whitespace character is a numbered line.
  968.        4) A TAB followed by a digit indicates a continuation line."
  969.   (save-excursion
  970.     (beginning-of-line)
  971.     (if (looking-at comment-line-start-skip)
  972.     (if fortran-comment-indent-style
  973.         (let ((char (if (stringp fortran-comment-indent-char)
  974.                 (aref fortran-comment-indent-char 0)
  975.               fortran-comment-indent-char)))
  976.           (goto-char (match-end 0))
  977.           (delete-horizontal-regexp (concat " \t" (char-to-string char)))
  978.           (insert-char char (- col (current-column)))))
  979.       (if (looking-at "\t[1-9]")
  980.       (if indent-tabs-mode
  981.           (goto-char (match-end 0))
  982.         (delete-char 2)
  983.         (insert "     ")
  984.         (insert fortran-continuation-string))
  985.     (if (looking-at "     [^ 0\n]")
  986.         (if indent-tabs-mode
  987.         (progn (delete-char 6)
  988.                (insert "\t")
  989.                (insert-char (fortran-numerical-continuation-char) 1))
  990.           (forward-char 6))
  991.       (delete-horizontal-space)
  992.       ;; Put line number in columns 0-4
  993.       ;; or put continuation character in column 5.
  994.       (cond ((eobp))
  995.         ((looking-at (regexp-quote fortran-continuation-string))
  996.          (if indent-tabs-mode
  997.              (progn
  998.                (indent-to 
  999.             (if indent-tabs-mode
  1000.                 fortran-minimum-statement-indent-tab
  1001.               fortran-minimum-statement-indent-fixed))
  1002.                (delete-char 1)
  1003.                (insert-char (fortran-numerical-continuation-char) 1))
  1004.            (indent-to 5)
  1005.            (forward-char 1)))
  1006.         ((looking-at "[0-9]+")
  1007.          (let ((extra-space (- 5 (- (match-end 0) (point)))))
  1008.            (if (< extra-space 0)
  1009.                (message "Warning: line number exceeds 5-digit limit.")
  1010.              (indent-to (min fortran-line-number-indent extra-space))))
  1011.          (skip-chars-forward "0-9")))))
  1012.       ;; Point is now after any continuation character or line number.
  1013.       ;; Put body of statement where specified.
  1014.       (delete-horizontal-space)
  1015.       (indent-to col)
  1016.       ;; Indent any comment following code on the same line.
  1017.       (if (and comment-start-skip
  1018.            (fortran-find-comment-start-skip))
  1019.       (progn (goto-char (match-beginning 0))
  1020.          (if (not (= (current-column) (fortran-comment-hook)))
  1021.              (progn (delete-horizontal-space)
  1022.                 (indent-to (fortran-comment-hook)))))))))
  1023.  
  1024. (defun fortran-line-number-indented-correctly-p ()
  1025.   "Return t if current line's line number is correctly indented.
  1026. Do not call if there is no line number."
  1027.   (save-excursion
  1028.     (beginning-of-line)
  1029.     (skip-chars-forward " \t")
  1030.     (and (<= (current-column) fortran-line-number-indent)
  1031.      (or (= (current-column) fortran-line-number-indent)
  1032.          (progn (skip-chars-forward "0-9")
  1033.             (= (current-column) 5))))))
  1034.  
  1035. (defun fortran-check-for-matching-do ()
  1036.   "When called from a numbered statement, returns t if matching DO is found.
  1037. Otherwise return a nil."
  1038.   (let (charnum
  1039.     (case-fold-search t))
  1040.     (save-excursion
  1041.       (beginning-of-line)
  1042.       (if (looking-at "[ \t]*[0-9]+")
  1043.       (progn
  1044.         (skip-chars-forward " \t")
  1045.         (skip-chars-forward "0") ;skip past leading zeros
  1046.         (setq charnum (buffer-substring (point)
  1047.                         (progn (skip-chars-forward "0-9")
  1048.                            (point))))
  1049.         (beginning-of-line)
  1050.         (and (re-search-backward
  1051.           (concat
  1052.            "\\(^[ \t0-9]*end\\b[ \t]*[^ \t=(a-z]\\)\\|\\(^[ \t0-9]*do\
  1053. [ \t]*0*"
  1054.            charnum "\\b\\)\\|\\(^[ \t]*0*" charnum "\\b\\)")
  1055.           nil t)
  1056.          (looking-at (concat "^[ \t0-9]*do[ \t]*0*" charnum))))))))
  1057.  
  1058. (defun fortran-find-comment-start-skip ()
  1059.   "Move to past `comment-start-skip' found on current line.
  1060. Return t if `comment-start-skip' found, nil if not."
  1061. ;;; In order to move point only if comment-start-skip is found,
  1062. ;;; this one uses a lot of save-excursions.  Note that re-search-forward
  1063. ;;; moves point even if comment-start-skip is inside a string-constant.
  1064. ;;; Some code expects certain values for match-beginning and end
  1065.   (interactive)
  1066.   (let ((save-match-beginning) (save-match-end))
  1067.     (if (save-excursion 
  1068.       (re-search-forward comment-start-skip
  1069.                  (save-excursion (end-of-line) (point)) t))
  1070.     (progn
  1071.       (setq save-match-beginning (match-beginning 0))
  1072.       (setq save-match-end (match-end 0))
  1073.       (if (fortran-is-in-string-p (match-beginning 0))
  1074.           (progn
  1075.         (save-excursion
  1076.           (goto-char save-match-end)
  1077.           (fortran-find-comment-start-skip)) ; recurse for rest of line
  1078.         )
  1079.         (goto-char save-match-beginning)
  1080.         (re-search-forward comment-start-skip
  1081.                    (save-excursion (end-of-line) (point)) t)
  1082.         (goto-char (match-end 0))
  1083.         t))
  1084.       nil)))
  1085.  
  1086. ;;;From: ralf@up3aud1.gwdg.de (Ralf Fassel)
  1087. ;;; Test if TAB format continuation lines work.
  1088. (defun fortran-is-in-string-p (where)
  1089.   "Return non-nil if POS (a buffer position) is inside a Fortran string,
  1090. nil else."
  1091.   (save-excursion
  1092.     (goto-char where)
  1093.     (cond
  1094.      ((bolp) nil)            ; bol is never inside a string
  1095.      ((save-excursion            ; comment lines too
  1096.     (beginning-of-line)(looking-at comment-line-start-skip)) nil)
  1097.      (t (let (;; ok, serious now. Init some local vars:
  1098.           (parse-state '(0 nil nil nil nil nil 0))
  1099.           (quoted-comment-start (if comment-start
  1100.                     (regexp-quote comment-start)))
  1101.           (not-done t)
  1102.           parse-limit
  1103.           end-of-line
  1104.           )
  1105.       ;; move to start of current statement
  1106.       (fortran-next-statement)
  1107.       (fortran-previous-statement)
  1108.       ;; now parse up to WHERE
  1109.       (while not-done
  1110.         (if (or ;; skip to next line if:
  1111.          ;; - comment line?
  1112.          (looking-at comment-line-start-skip)
  1113.          ;; - at end of line?
  1114.          (eolp)
  1115.          ;; - not in a string and after comment-start?
  1116.          (and (not (nth 3 parse-state))
  1117.               comment-start
  1118.               (equal comment-start
  1119.                  (char-to-string (preceding-char)))))
  1120.         ;; get around a bug in forward-line in versions <= 18.57
  1121.         (if (or (> (forward-line 1) 0) (eobp))
  1122.             (setq not-done nil))
  1123.           ;; else:
  1124.           ;; if we are at beginning of code line, skip any
  1125.           ;; whitespace, labels and tab continuation markers.
  1126.           (if (bolp) (skip-chars-forward " \t0-9"))
  1127.           ;; if we are in column <= 5 now, check for continuation char
  1128.           (cond ((= 5 (current-column)) (forward-char 1))
  1129.             ((and (< (current-column) 5)
  1130.               (equal fortran-continuation-string
  1131.                  (char-to-string (following-char)))
  1132.               (forward-char 1))))
  1133.           ;; find out parse-limit from here
  1134.           (setq end-of-line (save-excursion (end-of-line)(point)))
  1135.           (setq parse-limit (min where end-of-line))
  1136.           ;; parse max up to comment-start, if non-nil and in current line
  1137.           (if comment-start
  1138.           (save-excursion
  1139.             (if (re-search-forward quoted-comment-start end-of-line t)
  1140.             (setq parse-limit (min (point) parse-limit)))))
  1141.           ;; now parse if still in limits
  1142.           (if (< (point) where)
  1143.           (setq parse-state (parse-partial-sexp
  1144.                      (point) parse-limit nil nil parse-state))
  1145.         (setq not-done nil))
  1146.           ))
  1147.       ;; result is
  1148.       (nth 3 parse-state))))))
  1149.  
  1150. (defun fortran-auto-fill-mode (arg)
  1151.   "Toggle fortran-auto-fill mode.
  1152. With ARG, turn `fortran-auto-fill' mode on iff ARG is positive.
  1153. In `fortran-auto-fill' mode, inserting a space at a column beyond `fill-column'
  1154. automatically breaks the line at a previous space."
  1155.   (interactive "P")
  1156.   (prog1 (setq auto-fill-function
  1157.            (if (if (null arg)
  1158.                (not auto-fill-function)
  1159.              (> (prefix-numeric-value arg) 0))
  1160.            'fortran-indent-line
  1161.          nil))
  1162.     ;; update mode-line
  1163.     (set-buffer-modified-p (buffer-modified-p))))
  1164.  
  1165. (defun fortran-do-auto-fill ()
  1166.   (interactive)
  1167.   (let* ((opoint (point))
  1168.      (bol (save-excursion (beginning-of-line) (point)))
  1169.      (eol (save-excursion (end-of-line) (point)))
  1170.      (bos (min eol (+ bol (fortran-current-line-indentation))))
  1171.      (quote
  1172.       (save-excursion
  1173.         (goto-char bol)
  1174.         (if (looking-at comment-line-start-skip)
  1175.         nil            ; OK to break quotes on comment lines.
  1176.           (move-to-column fill-column)
  1177.           (cond ((fortran-is-in-string-p (point))
  1178.              (save-excursion (re-search-backward "[^']'[^']" bol t)
  1179.                      (if fortran-break-before-delimiters
  1180.                      (point)
  1181.                        (1+ (point)))))
  1182.             (t nil)))))
  1183.      ;;
  1184.      ;; decide where to split the line. If a position for a quoted
  1185.      ;; string was found above then use that, else break the line
  1186.      ;; before the last delimiter.
  1187.      ;; Delimiters are whitespace, commas, and operators.
  1188.      ;; Will break before a pair of *'s.
  1189.      ;;
  1190.      (fill-point
  1191.       (or quote
  1192.           (save-excursion
  1193.         (move-to-column (1+ fill-column))
  1194.         (skip-chars-backward "^ \t\n,'+-/*=)"
  1195. ;;;         (if fortran-break-before-delimiters
  1196. ;;;             "^ \t\n,'+-/*=" "^ \t\n,'+-/*=)")
  1197.          )
  1198.         (if (<= (point) (1+ bos))
  1199.             (progn
  1200.               (move-to-column (1+ fill-column))
  1201. ;;;what is this doing???
  1202.               (if (not (re-search-forward "[\t\n,'+-/*)=]" eol t))
  1203.               (goto-char bol))))
  1204.         (if (bolp)
  1205.             (re-search-forward "[ \t]" opoint t)
  1206.           (forward-char -1)
  1207.           (if (looking-at "'")
  1208.               (forward-char 1)
  1209.             (skip-chars-backward " \t\*")))
  1210.         (if fortran-break-before-delimiters
  1211.             (point)
  1212.           (1+ (point))))))
  1213.      )
  1214.     ;; if we are in an in-line comment, don't break unless the
  1215.     ;; line of code is longer than it should be. Otherwise
  1216.     ;; break the line at the column computed above.
  1217.     ;;
  1218.     ;; Need to use fortran-find-comment-start-skip to make sure that quoted !'s
  1219.     ;; don't prevent a break.
  1220.     (if (not (or (save-excursion
  1221.            (if (and (re-search-backward comment-start-skip bol t)
  1222.                 (not (fortran-is-in-string-p (point))))
  1223.                (progn
  1224.              (skip-chars-backward " \t")
  1225.              (< (current-column) (1+ fill-column)))))
  1226.          (save-excursion
  1227.            (goto-char fill-point)
  1228.            (bolp))))
  1229.     (if (> (save-excursion
  1230.          (goto-char fill-point) (current-column))
  1231.            (1+ fill-column))
  1232.         (progn (goto-char fill-point)
  1233.            (fortran-break-line))
  1234.       (save-excursion
  1235.         (if (> (save-excursion
  1236.              (goto-char fill-point) 
  1237.              (current-column))
  1238.            (+ (calculate-fortran-indent) fortran-continuation-indent))
  1239.         (progn
  1240.           (goto-char fill-point)
  1241.           (fortran-break-line))))))
  1242.     ))
  1243. (defun fortran-break-line ()
  1244.   (let ((opoint (point))
  1245.     (bol (save-excursion (beginning-of-line) (point)))
  1246.     (eol (save-excursion (end-of-line) (point)))
  1247.     (comment-string nil))
  1248.     
  1249.     (save-excursion
  1250.       (if (and comment-start-skip (fortran-find-comment-start-skip))
  1251.       (progn
  1252.         (re-search-backward comment-start-skip bol t)
  1253.         (setq comment-string (buffer-substring (point) eol))
  1254.         (delete-region (point) eol))))
  1255. ;;; Forward line 1 really needs to go to next non white line
  1256.     (if (save-excursion (forward-line 1)
  1257.             (or (looking-at "     [^ 0\n]")
  1258.                 (looking-at "\t[1-9]")))
  1259.     (progn
  1260.       (forward-line 1)
  1261.       (delete-indentation)
  1262.       (delete-char 2)
  1263.       (delete-horizontal-space)
  1264.       (fortran-do-auto-fill))
  1265.       (fortran-split-line))
  1266.     (if comment-string
  1267.     (save-excursion
  1268.       (goto-char bol)
  1269.       (end-of-line)
  1270.       (delete-horizontal-space)
  1271.       (indent-to (fortran-comment-hook))
  1272.       (insert comment-string)))))
  1273.  
  1274. (defun fortran-analyze-file-format ()
  1275.   "Returns nil if fixed format is used, t if TAB formatting is used.
  1276. Use `fortran-tab-mode-default' if no non-comment statements are found in the
  1277. file before the end or the first `fortran-analyze-depth' lines."
  1278.   (let ((i 0))
  1279.     (save-excursion
  1280.       (goto-char (point-min))
  1281.       (setq i 0)
  1282.       (while (not (or
  1283.            (eobp)
  1284.            (looking-at "\t")
  1285.            (looking-at "      ")
  1286.            (> i fortran-analyze-depth)))
  1287.     (forward-line)
  1288.     (setq i (1+ i)))
  1289.       (cond
  1290.        ((looking-at "\t") t)
  1291.        ((looking-at "      ") nil)
  1292.        (fortran-tab-mode-default t)
  1293.        (t nil)))))
  1294.  
  1295. (or (assq 'fortran-tab-mode-string minor-mode-alist)
  1296.     (setq minor-mode-alist (cons
  1297.                 '(fortran-tab-mode-string
  1298.                   (indent-tabs-mode fortran-tab-mode-string))
  1299.                 minor-mode-alist)))
  1300.  
  1301. (provide 'fortran)
  1302.  
  1303. ;;; fortran.el ends here
  1304.  
  1305.